<?php
namespace app\api\controller;

class Product extends Base
{

    public function _initialize()
    {
        parent::_initialize();
        //$this->chkToken();
    }
    // 获取分类
    public function get_pools()
    {
        $post = array();
        $data = $this->sendInfo('categoryThree', $post);
        $return['result'] = array();
        foreach ($data['data'] as $key => $val) {
            $temp['id'] = $val['api_option'];
            $temp['name'] = $val['category_name'];
            array_push($return['result'], $temp);
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function skus()
    {
        if (! isset($this->request['catalog_id'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入分类池编号';
            $this->ajaxReturn($return);
        }
        $post_category['api_option'] = $this->request['catalog_id'];
        $get_category = $this->sendInfo('apiCategory', $post_category);
        $category_sign3 = array();
        foreach ($get_category['data'] as $key => $val) {
            array_push($category_sign3, $val['category_sign']);
        }
        $post['where']['category_sign3'] = $category_sign3;
        $post['page']['page'] = 1;
        $post['page']['limit'] = 10000;
        $post['sort']['_o'] = 'create_time';
        $post['sort']['_b'] = 'asc';
        $post['no_need_handle_data'] = 'yes';
        $data = $this->sendInfo('goodsList', $post);
        $return['result'] = array();
        foreach ($data['data'] as $key => $val) {
            array_push($return['result'], $val['goods_sku']);
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function detail()
    {
        if (! isset($this->request['sku'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入商品编号';
            $this->ajaxReturn($return);
        }
        $post['goods_sku'] = $this->request['sku'];
        $post['member_id'] = $this->member_id;
        $post['site_id'] = $this->ys_config['siteId'];
        // 设置采集状态1
        $post['api_get_add_set'] = 1;
        $data = $this->sendInfo('goodsOnlyDetail', $post);
        if(!isset($data['data']['goods_id'])){
            $return['success'] = false;
                $return['desc'] = '商品已删除';
                $this->ajaxReturn($return);
        }
        $e = $data['data'];
        $temp['sku'] = $e['goods_sku'];
        $temp['url'] = $this->site_url . 'goods_detail.html?goods_sku='.$e['goods_sku'];
        $temp['typeno'] = $e['goods_model'];
        $temp['image_path'] = $e['goods_picture']['save_path'];
        $temp['state'] = $e['sale_state'] == 1 ? 1 : 0;
        $temp['brand_id'] = $e['brand_sign'];
        $temp['brand_name'] = $e['brand_name'];
        $temp['name'] = $e['goods_name'];
        $temp['unit'] = $e['goods_unit'];
        $temp['category'] = $e['category_api_option'];
        $temp['deliveryDay'] = '3';
        $temp['service'] = '无';
        $temp['ware'] = '原厂包装';
        
        $attr_head = '';
        $t_param = '<table class="Ptable">';
        $main_property_arigin = array();
        foreach ($e['goods_attr'] as $key => $val) {
            if ($val['group_name'] != $attr_head) {
                $t_param .= '<tr> <th colspan="2">' . $val['group_name'] . '</th></tr>';
                $attr_head = $val['group_name'];
            }
            $t_param .= '<tr> <td class="tdTitle">' . $val['attr_name'] . '</td> <td>' . $val['attr_value'] . '</td> </tr>';
            if($val['group_name'] == '基础参数'){
                $main_property_arigin[$val['attr_name']] = $val['attr_value'];
            }
            $value = $val['attr_value'];
            switch ($val['attr_name']) {
                case '品牌':
                    $main_property_arigin[$val['attr_name']] = $temp['brand_name'];
                    break;
                case '简要配置':
                    $temp['briefconfig'] = $value;
                    break;
                case '节能':
                    $temp['energy'] = $value;
                    break;
                case '环保':
                    $temp['protection'] = $value;
                    break;
                case '是否是进口产品':
                    $temp['Imported'] = $value;
                    break;
                case '节能品牌':
                    $temp['EfficientGoodsBrand'] = $value;
                    break;
                case '节能型号':
                    $temp['EfficientGoodsXinhao'] = $value;
                    break;
                case '节能标志认证证书号':
                    $temp['EfficientGoodsSn'] = $value;
                    break;
                case '认证证书有效截至日期':
                    $temp['EfficientGoodsExpiry'] = $value;
                    break;
                case '环保品牌':
                    $temp['EnvmFriendlyGoodsBrand'] = $value;
                    break;
                case '环保型号':
                    $temp['EnvmFriendlyGoodsXinhao'] = $value;
                    break;
                case '环保标志认证证书号':
                    $temp['EnvmFriendlyGoodsSn'] = $value;
                    break;
                case '环保证书有效截至日期':
                    $temp['EnvmFriendlyGoodsExpiry'] = $value;
                    break;
                case '计算机配置':
                    $temp['pc_param'] = $value;
                    break;
                case '节能产品证书编号':
                    $temp['EfficientGoodsCardNum'] = $value;
                    break;
                case '节能产品证书图片':
                    $temp['EfficientGoodsImagePath'] = $value;
                    break;
                case '节能产品证书认证机构':
                    $temp['EfficientGoodsCardOrgan'] = $value;
                    break;
                case '环保产品证书编号':
                    $temp['EnvmFriendlyGoodsCardNum'] = $value;
                    break;
                case '环保产品证书图片':
                    $temp['EnvmFriendlyGoodsImagePath'] = $value;
                    break;
                case '环保产品证书认证机构':
                    $temp['EnvmFriendlyGoodsCardOrgan'] = $value;
                    break;
                case '服务':
                    $temp['service'] = $value;
                    break;
                case '特色商品':
                    if($value =='货柜机'){
                        $temp['is_cabinet'] = 1;
                    } elseif($value =='消防器材'){
                        $temp['is_cabinet'] = 2;
                    } else {
                        $temp['is_cabinet'] = 0;
                    }
                    break;
            }
        }
        $temp['main_property_arigin'] = $main_property_arigin;
        if($e['api_option'] == 'A0101'){
            $temp['pc_param'] = $main_property_arigin;
        }
        $t_param .= '</table>';
         $temp['introduction'] = $e['goods_content'];
        $temp['param'] = $t_param;
        /*
        $temp['sale_actives'] = 0;
        $temp['energy'] = '否';
        $temp['protection'] = '否';
        $temp['briefconfig'] = '';
        $temp['ware'] = '';
        $temp['deliveryDay'] = 3;
        $temp['Imported'] = '非进口产品';
        */
        $return['result'] = $temp;
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function shelf_states()
    {
        if (! isset($this->request['sku'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入商品编号';
            $this->ajaxReturn($return);
        }
        $post['where']['goods_sku'] = explode(',', $this->request['sku']);
        $post['page']['page'] = 1;
        $post['page']['limit'] = 10000;
        $post['sort']['_o'] = 'create_time';
        $post['sort']['_b'] = 'desc';
        $post['no_need_handle_data'] = 'yes';
        $post['sale_state'] = 'all';
        $data = $this->sendInfo('goodsList', $post);
        $return['result'] = array();
        foreach ($data['data'] as $key => $val) {
            $temp['sku'] = $val['goods_sku'];
            $temp['state'] = $val['sale_state'] == 1 ? 1 : 0;
            array_push($return['result'], $temp);
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function images()
    {
        if (! isset($this->request['sku'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入商品编号';
            $this->ajaxReturn($return);
        }
        $post['where']['goods_sku'] = explode(',', $this->request['sku']);
        $post['page']['page'] = 1;
        $post['page']['limit'] = 10000;
        $post['sort']['_o'] = 'create_time';
        $post['sort']['_b'] = 'desc';
        $data = $this->sendInfo('goodsList', $post);
        $return['result'] = array();
        foreach ($data['data'] as $key => $val) {
            $temp['sku'] = $val['goods_sku'];
            $img_sort = 0;
            $temp['images'] = array();
            foreach ($val['goods_pictures'] as $k => $v) {
                $t1['order'] = $img_sort ++;
                $t1['path'] = $v['save_path'];
                array_push($temp['images'], $t1);
            }
            array_push($return['result'], $temp);
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function prices()
    {
        if (! isset($this->request['sku'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入商品编号';
            $this->ajaxReturn($return);
        }
        $post['where']['goods_sku'] = explode(',', $this->request['sku']);
        $post['page']['page'] = 1;
        $post['page']['limit'] = 10000;
        $post['sort']['_o'] = 'create_time';
        $post['sort']['_b'] = 'desc';
        $data = $this->sendInfo('goodsList', $post);
        $return['result'] = array();
        foreach ($data['data'] as $key => $val) {
            $temp['sku'] = $val['goods_sku'];
            $temp['price'] = floatval($val['api_price']);
            $temp['mall_price'] = floatval($val['sale_price']);
            array_push($return['result'], $temp);
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function stocks()
    {
        if (! isset($this->request['sku'])) {
            $return['success'] = false;
            $return['desc'] = '没有传入商品编号';
            $this->ajaxReturn($return);
        }
        $post['where']['goods_sku'] = explode(',', $this->request['sku']);
        $post['page']['page'] = 1;
        $post['page']['limit'] = 10000;
        $post['sort']['_o'] = 'create_time';
        $post['sort']['_b'] = 'desc';
        $data = $this->sendInfo('goodsList', $post);
        $return['result'] = array();
        foreach ($data['data'] as $key => $val) {
            $temp['sku'] = $val['goods_sku'];
            $temp['area'] = $this->request['area'];
            $temp['desc'] = '有货';
            $temp['num'] = $val['view_stock'];
            array_push($return['result'], $temp);
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function addNewSkus()
    {
        $post['member_id'] = $this->member_id;
        $post['where']['api_get_add'] = 'yes';
        $post['sort']['_o'] = 'create_time';
        $post['sort']['_b'] = 'asc';
        $post['page']['page'] = 1;
        $post['page']['limit'] = 10000;
        $post['no_need_handle_data'] = 'yes';
        $post['site_id'] = $this->ys_config['siteId'];
        $data = $this->sendInfo('goodsList', $post);
        $return['result'] = array();
        if ($data['count'] > 0) {
            foreach ($data['data'] as $key => $val) {
                array_push($return['result'], $val['goods_sku']);
            }
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }

    public function editSkus()
    {
        $post['member_id'] = $this->member_id;
        $post['where']['api_get_add'] = 'yes';
        $post['sort']['_o'] = 'goods_id';
        $post['sort']['_b'] = 'asc';
        $post['page']['page'] = 1;
        $post['page']['limit'] = 10000;
        $post['no_need_handle_data'] = 'yes';
        $post['site_id'] = $this->ys_config['siteId'];
        $data = $this->sendInfo('goodsList', $post);
        $return['result'] = array();
        if ($data['count'] > 0) {
            foreach ($data['data'] as $key => $val) {
                array_push($return['result'], $val['goods_sku']);
            }
        }
        $return['success'] = true;
        $this->ajaxReturn($return);
    }
}